//
//  CarFilterModel].swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/1/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit


struct CarFilterModel {
    var carFilter: CarFilter
    
    var inputs: [Input]! {
        if carFilter.marka != nil {
            var initial = [
                Input(type: "marka", value: carFilter.marka?.name_ru, placeholder: "Марка", icon: UIImage(named: "car"), values: nil),
                Input(type: "model", value: self.getModelName(), placeholder: "Модель", icon: UIImage(named: "model"), values: carFilter.models)
            ]
            /// - <#Поколение#>
            if (carFilter.models.count != 0) {
                initial.append(Input(type: "generation", value: self.getGenerationName(), placeholder: "Поколение, Серия, Модификация", icon: UIImage(named: "car"), values: nil))
            }
            return initial
        } else {
            return [Input(type: "marka", value: carFilter.marka?.name_ru, placeholder: "Марка, модель", icon: UIImage(named: "car"), values: nil) ]
        }
    }
    
    
    private func getModelName () -> String? {
        if (carFilter.models.count == 1) {
            return carFilter.models[0].name_ru
        } else if carFilter.models.count == 0{
            return nil
        } else {
            return carFilter.models.map {
                $0.name_ru
            }.joined(separator: ", ")
        }
    }
    
    private func getGenerationName () -> String? {
        if (carFilter.models.count == 0) {
            return nil
        } else {
            if (self.findGeneration()) {
                var pokoleniyaTexts:[String] = []
                for model in carFilter.models {
                    pokoleniyaTexts += (model.generation ?? []).map {
                        $0.name
                    }
                }
                
                return "Поколения: " + pokoleniyaTexts.joined(separator: ", ") + ", Серии: " + self.findSerie() + ", Модификации: " + self.findModifications()
            } else {
                return "Все поколения"
            }
        }
    }
    
    
    
    // check if has at least one generation
    private func findGeneration () -> Bool {
        var found = false
        var index = 0
        while (!found && index <= carFilter.models.count - 1) {
            found = carFilter.models[index].generation != nil && carFilter.models[index].generation!.count != 0
            index += 1
        }
        return found
    }
    // check if has at least one serie
    
    private func findSerie () -> String {
        var names: [String] = []
        
        for model in carFilter.models {
            for generation in (model.generation ?? []) {
                for serie in (generation.series ?? []) {
                    names.append(serie.name)
                }
            }
        }
        return names.joined(separator: ", ")
    }
    
    private func findModifications () -> String {
        var names: [String] = []
        
        for model in carFilter.models {
            for generation in (model.generation ?? []) {
                for serie in (generation.series ?? []) {
                    for modification in serie.modifications ?? [] {
                        names.append(modification.name)
                    }
                }
            }
        }
        
        return names.joined(separator: ", ")
    }
    

    init (carFilter: CarFilter) {
        self.carFilter = carFilter
    }
}
